/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.io.PrintStream;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 *
 * @author caos
 */

public class ClientWriter extends Thread{
    private ConcurrentLinkedQueue<String> inQueue;
    private PrintStream pS;
    ClientWriter(PrintStream pS)
    {
        inQueue=new ConcurrentLinkedQueue();
        this.pS=pS;
    }
    
    void addInQueue(String toAdd)
    {
        inQueue.add(toAdd);
        if(toAdd.startsWith("LOGOUT"))
        {
            pS.println(toAdd + (char)4);
            this.interrupt();
        }
        
    }
    
    @Override
    public void run()
    {
        while(true)
        {
            try
            {
                if(!inQueue.isEmpty())
                {
                    String msg=inQueue.poll();
                    if(!msg.isEmpty())
                    {
                        System.out.println("<<< " + msg);
                        pS.println(msg + (char)4);
                    }
                }
                else
                     sleep(200);
            } 
            catch (InterruptedException ex) {
                System.out.println("logout...");
                return;
            }  
            catch (Exception ex) 
            {
                System.err.println("Error: " + ex.getMessage());
            }
        }
    }
}
